package com.lagou.task17;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamTest {
    public static void main(String[] args) {
//        1.创建 ObjectOutputStream对象关联文件
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream("a.txt"));
            //        2.准备一个User对象并序列化
            User user = new User("qidian","123456","11111111");
//        3.将整个对象写入到文件
            objectOutputStream.writeObject(user);
            System.out.println("写入User对象");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //        4.关闭对象流并释放有关的资源
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }


    }
}
